(function(){
    "use strict";
    app.controller('hostCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = '资源管理 > 主机';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $scope.searchByHostname="";
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取主机列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        hostName:$scope.searchByHostname
                    };
                httpLoad.loadData({
                    url:'/runtime/host/list',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.hostList = data.rows;
                            $scope.totalCount = data.total;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //跳转详情页
            $scope.detail = function(item){
                $state.go('app.resources.hostDetail',{id:item.hostId,hostRealName:item.hostRealName,flag:0});
            };
            //新增
            $scope.add = function(){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/host/add.html',
                    controller : 'addHostModalCtrl'// 初始化模态范围
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //编辑
            $scope.update = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/host/update.html',  //指向上面创建的视图
                    controller : 'updateHostModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //删除
            $scope.remove = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/host/delete.html',
                    controller : 'deleteHostModalCtrl',
                    resolve : {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
        }
    ]);
    //新增ctrl
    angular.module('app').controller('addHostModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
        function($scope,$modalInstance,LANGUAGE,httpLoad){
            $scope.sites = [
                {site : "计算节点（Kube-Docker）", val : "1"},
                {site : "控制节点（Kubernetes）", val : "0"},
                {site : "软件仓库（Docker-Registry）", val : "2"}
            ];
             $scope.addData = {
                  hostCreator:"1",
             }
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/runtime/host/create',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                        	 $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //编辑ctrl
    angular.module('app').controller('updateHostModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
            $scope.updateData = {};
            $scope.updateData.hostName = itemData.hostName;
            $scope.ok = function(){
                $scope.updateData.hostId = itemData.hostId;
                httpLoad.loadData({
                    url:'/runtime/host/modify',
                    data: $scope.updateData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //删除ctrl
    angular.module('app').controller('deleteHostModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/runtime/host/remove',
                    data:{hostId: id},
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
})();